package com.yx.shop.web.controller;

import com.yx.shop.common.Const;
import com.yx.shop.entitly.Cart;
import com.yx.shop.entitly.User;
import com.yx.shop.service.CartService;
import com.yx.shop.service.impl.CartServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Classname Cartcontroller
 * @Description TODO:
 * @Date 2021/05/07 21:23
 * @Created by yx
 */
@WebServlet("/cart")
public class Cartcontroller extends BaseServlet{
    private CartService cartService = new CartServiceImpl();

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/08 11:39
     * @param request
     * @param response
     * @Description:将商品添加至购物车
     **/
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if(user==null){
            request.getSession().setAttribute("msg","添加购物车必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT+"/login.jsp";
        }
        //2.获取购物车所需参数--->用户id,商品id
        Integer uid = user.getUid();
        Integer pid = Integer.valueOf(request.getParameter("pid"));

        cartService.createCart(uid,pid);

        return Const.FORWORD+"/cartSuccess.jsp";
    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/08 17:25
     * @param request
     * @param response
     * @Description:获取该用户购物车所有商品数据
     **/
    public String getCart(HttpServletRequest request,HttpServletResponse response){
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user==null){
            request.getSession().setAttribute("msg","查看购物车必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT+"/login.jsp";
        }
        //2.获取参数 用户id--->用于查询该用户购物车
        Integer uid = user.getUid();
        List<Cart> cartList = cartService.getCartByUid(uid);
        //3.存储购物车信息
        request.setAttribute(Const.CartInfo.CART_LIST,cartList);
        return Const.FORWORD+"/cart.jsp";

    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/08 19:10
     * @param request
     * @param response
     * @Description:清空购物车或删除单个商品
     **/
    public String clearCart(HttpServletRequest request,HttpServletResponse response){
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user==null){
            request.getSession().setAttribute("msg","删除购物车必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT+"/login.jsp";
        }
        //2.接收参数---pid
        Integer pid = Integer.valueOf(request.getParameter("pid"));
        boolean flag = cartService.clearCart(pid,user.getUid());
        //3.删除完后,重新查询购物车数据
        //重新请求到数据展示请求
        return Const.REDIRECT + "/cart?method=getCart";
    }

    public String updateCart(HttpServletRequest request,HttpServletResponse response){
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user==null){
            request.getSession().setAttribute("msg","修改购物车必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT+"/login.jsp";
        }
        //2.获取参数
        Integer pid = Integer.valueOf(request.getParameter("pid"));
        Integer num = Integer.valueOf(request.getParameter("num"));
        Double pprice = Double.valueOf(request.getParameter("price"));
        //num 1 0 -1
        // 数量+1 删除 数量-1
        //3.调用业务操作
        Boolean ans = cartService.updateCart(user.getUid(),pid,num,pprice);
        return ans.toString();
    }
}
